﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace DFGK.Service
{
    public class PalletService : BaseService, IPalletService
    {
        private readonly IPalletRepository _pallrepository;
        private readonly IInventoryRepository _inventoryrepository;
        private readonly IStackingStorageRepository _stackingstoragerepository;
        private readonly IPutawayOrderRepository _putawayorderrepository;
        private readonly IUnitOfWork _unitOfWork;
        public PalletService(IMapper mapper, IPalletRepository _pallrepository, IInventoryRepository _inventoryrepository, IUnitOfWork unitOfWork, IStackingStorageRepository stackingstoragerepository, IPutawayOrderRepository putawayorderrepository) : base(mapper)
        {
            _unitOfWork = unitOfWork;
            this._pallrepository = _pallrepository;
            this._inventoryrepository = _inventoryrepository;
            _stackingstoragerepository = stackingstoragerepository;
            _putawayorderrepository = putawayorderrepository;
        }

        public bool AddPallet(Pallet dto, out string message)
        {
            message = "";
            var sobj = _pallrepository.QueryIsEntityWhere(x => x.PalletBarcode == dto.PalletBarcode);
            if (sobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            _pallrepository.AddEntity(dto);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool AddPalletList(BatchPalletDTO m, out string message)
        {
            message = string.Empty;
            string name = "";
            if (m.Type == 0)
            {
                name = "托盘-";
            }
            else if (m.Type == 1)
            {
                name = "料箱800*600-";
            }
            else if (m.Type == 2)
            {
                name = "料箱600*400-";
            }

            _unitOfWork.BeginTransaction();
            try
            {
                var existingOrderIds = _pallrepository.GetEntityAll().Select(x => x.PalletBarcode).ToList();
                string pattern = @"\d+";
                MatchCollection matches = Regex.Matches(m.BeginBarcode, pattern);
                //获取最后一个数字
                string value = matches[matches.Count - 1].Value;
                int values = Convert.ToInt32(value);
                //当前数字长度
                int valuelenth = value.Length;
                //总长度
                int allvaluelenth = m.BeginBarcode.Length;
                //托盘名称
                string name1 = m.BeginBarcode.Substring(0, allvaluelenth - valuelenth);
                for (int i = 0; i < m.Num; i++)
                {
                    Pallet p = new Pallet();
                    p.Id = YitIdHelper.NextId();
                    p.PalletName = string.Concat(name, values.ToString("D3"));
                    p.PalletBarcode = values.ToString();
                    p.Type = m.Type;
                    p.Opprint = m.IsPrint;
                    p.CreateDate = DateTime.Now;
                    p.Status = 1;
                    if (existingOrderIds.Contains(p.PalletBarcode))
                    {
                        message += "托盘条码" + p.PalletBarcode + "已存在";
                        continue;
                    }
                    _pallrepository.AddEntity(p);
                    values++;
                }
                if(_unitOfWork.SaveChangesEntity())
                {
                    _unitOfWork.CommitChanges();
                    return true;
                }
                _unitOfWork.RollbackChanges();
                return false;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                _unitOfWork.RollbackChanges();
                return false;
                throw;
            }
        }

        public bool DelPallet(long id)
        {
            var del = _pallrepository.QueryEntityById(id);
            if (del != null)
            {
                _pallrepository.DelEntity(del);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public Pallet? GetPalletByid(long id)
        {
            return _pallrepository.QueryEntityById(id);
        }

        public List<Pallet> GetPallets(string? name, int type, long incell, int pageIndex, int pageSize, out int totalCount)
        {
            var list = _pallrepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.PalletName.Contains(name));
            }
            if (incell > 0)
            {
                if (incell == 1)
                {
                    list = list.Where(x => x.LocationCode == null);
                }
                if (incell == 2)
                {
                    list = list.Where(x => x.LocationCode != null);
                }
            }

            if (type != -1)
            {
                list = list.Where(x => x.Type == type);
            }
            totalCount = list.Count();
            var lists = _pallrepository.GetEntityAlls(list, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return lists.ToList();
        }

        public List<Pallet> GetTypePallet(int palletType)
        {
            return _pallrepository.GetEntityWhereAll(p => p.Type == palletType).ToList();
        }

        public bool PalletVerify(string PalletCode, out string message)
        {
            message = string.Empty;
            var pallet = _pallrepository.QueryIsEntityWhere(p => p.PalletBarcode == PalletCode);
            if (pallet == null || pallet.Status==0) 
            {
                message = "托盘不存在或已被禁用！";
                return false;
            }
            //var sobj = _stackingstoragerepository.QueryIsEntityWhere(x => x.PalletBarcode == PalletCode);
            //if (sobj != null)
            //{
            //    message = "托盘正在作业中";
            //    return false;
            //}
            //var putobj = _putawayorderrepository.QueryIsEntityWhere(x => x.PalletBarcode == PalletCode && x.Status == 4 && x.BillType == 1 && x.SourceType == 1);
            //if(putobj!=null)
            //{
            //    message = "当前托盘正在作业中！";
            //    return false;
            //}
            var inventory = _inventoryrepository.QueryIsEntityWhere(r => r.PalletBarcode == PalletCode && r.Status==1);
            if (inventory is not null)
            {
                message = "当前托盘已在库存中！";
                return false;
            }
            return true;
        }

        public bool UpdPallet(Pallet dto, out string message)
        {
            message = "";
            var list = _pallrepository.QueryIsEntityWhere(x => x.Id != dto.Id && x.PalletBarcode == dto.PalletBarcode);

            if (list != null)
            {
                message = "编码已存在！";
                return false;
            }
            _pallrepository.UpdEntity(dto);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
